package org.fuxia.im.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.fuxia.im.common.ClientConst;
import org.fuxia.im.common.ClientContext;
import org.fuxia.im.core.ECMessageType;
import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.socket.impl.ECNioClient;
import org.fuxia.im.entities.User;
import org.tony.common.ui.CommonFrame;


public class AddFriend extends CommonFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField remarkName;
	private User user ; 
	private Formater[] formaters ;
	private JComboBox<Formater> group  ;
	private JDialog createGroupD ;
	private DefaultComboBoxModel<Formater> model  ;
	private Formater selectFormater ;
	private String selectGroupId ;
	private String selectGroupName ;
	

	public AddFriend(int width, int height, int x, int y, final String[] user, final int status) {
		super();
		setResizable(false);
		setTitle(ClientContext.getLoginUser().getNickName()+" - 添加好友");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(x+(width-460)/2, y+(height-340)/2, 460, 340);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContent(contentPane);
		contentPane.setLayout(null);
		ImageIcon icon = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("images/lifangfang.jpg")) ;
		icon.setImage(icon.getImage().getScaledInstance(108, 108, Image.SCALE_DEFAULT));

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(120, 0, 334, 276);
		contentPane.add(panel);
		panel.setLayout(null);

		JLabel remarkNameLab = new JLabel("备注名称：");
		remarkNameLab.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		remarkNameLab.setBounds(21, 22, 66, 22);
		panel.add(remarkNameLab);

		remarkName = new JTextField(user[1]);
		remarkName.setBounds(84, 23, 172, 21);
		panel.add(remarkName);
		remarkName.setColumns(10);

		JLabel groupLab = new JLabel("分    组：");
		groupLab.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		groupLab.setBounds(21, 54, 66, 22);
		panel.add(groupLab);
		
		String[] groups = (String[]) ClientContext.getContext().get("groups") ;
		formaters = new Formater[groups.length] ;
		for(int i=0; i<formaters.length;i++ ){
			String[] groupInfo = groups[i].split("#") ;
			Formater formater = new Formater(groupInfo[1], groupInfo[0]) ;
			formaters[i] = formater ;
		}

		
		
		group = new JComboBox<Formater>(formaters);
		model = (DefaultComboBoxModel<Formater>) group.getModel() ; 
		selectFormater = (Formater) model.getSelectedItem();
		selectGroupId = selectFormater.getGroupId() ;
		selectGroupName = selectFormater.getGroupName() ;
		group.setFont(new Font("微软雅黑", Font.PLAIN, 12)) ;
		group.setBounds(84, 54, 172, 21);
		panel.add(group);
		
		group.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectFormater = (Formater) model.getSelectedItem();
				selectGroupId = selectFormater.getGroupId() ;
				selectGroupName = selectFormater.getGroupName() ;
			}
		}) ;

		createGroupD = new JDialog(getFrame(), "创建分组", true);
		JButton createGroup = new JButton("新建分组");
		createGroup.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		createGroup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createGroupD.setBounds(getFrame().getX()+(getFrame().getWidth()-330)/2, getFrame().getY()+(getFrame().getHeight()-150)/2,330,150) ;
				createGroupD.getContentPane().setLayout(null) ;

				JLabel groupNameLab = new JLabel("分组名称：");
				groupNameLab.setFont(new Font("微软雅黑", Font.PLAIN, 12));
				groupNameLab.setBounds(20, 5, 88, 21);
				createGroupD.getContentPane().add(groupNameLab);

				final JTextField groupNameTF = new JTextField();
				groupNameTF.setFont(new Font("微软雅黑", Font.PLAIN, 12));
				groupNameTF.setBounds(20, 28, 270, 21);
				createGroupD.getContentPane().add(groupNameTF);
				groupNameTF.setColumns(10);

				JButton ok = new JButton("确定");
				ok.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String groupName = groupNameTF.getText() ;
						if("".equals(groupName) || groupName == null){
							groupNameTF.setText("请输入分组名称");
						}else if(isExistGroup(groupName)){
							JOptionPane.showMessageDialog(null, "您好，不能存在同名分组，请重命名！", "分组重名", JOptionPane.OK_OPTION);
						}else{
							ECPacket packet = new ECPacket() ;
							packet.setMessageType(ECMessageType.USER_GROUP.name()) ;
							packet.addParam("option", "add") ;
							packet.addParam("groupName", groupName) ;
							packet.addParam("userCode", ClientContext.getLoginUser().getUserCode()) ;
							
							packet.setFrom(ClientContext.getLoginUser().getUserCode()) ;
							ClientContext.putValue(ClientConst.CURRENT_REQUESTOR, getFrame()) ;
							ECNioClient client = ClientContext.getContext().getClient() ;
							try{
								client.sendMessage(packet) ;
							}catch(Exception e1){
								e1.printStackTrace() ;
							}
						}
					}
				}) ;
				ok.setFont(new Font("微软雅黑", Font.PLAIN, 12));
				ok.setBounds(175, 79, 60, 23);
				createGroupD.getContentPane().add(ok);

				JButton cancel = new JButton("取消");
				cancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						createGroupD.dispose();
					}
				}) ;
				cancel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
				cancel.setBounds(242, 79, 60, 23);
				createGroupD.getContentPane().add(cancel);
				createGroupD.setVisible(true);

			}
		});
		createGroup.setMargin(new Insets(0, 0, 0, 0));
		createGroup.setBounds(266, 54, 64, 23);
		panel.add(createGroup);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.WHITE);
		panel_1.setBorder(UIManager.getBorder("FormattedTextField.border"));
		panel_1.setBounds(0, 276, 454, 31);
		contentPane.add(panel_1);
		panel_1.setLayout(null);

		JButton next = new JButton("下一步");
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				ECPacket packet = new ECPacket() ;
				packet.setMessageType(ECMessageType.USER_FRIEND.name()) ;
				packet.addParam("option", "addFriend") ;
				packet.addParam("groupId", selectGroupId) ;
				packet.addParam("groupName", selectGroupName) ;
				packet.addParam("userName", user[1]) ;
				packet.addParam("status", Integer.toString(status)) ;
				packet.addParam("remarkName", remarkName.getText()) ;
				packet.setTo(user[2]) ;
				packet.setFrom(ClientContext.getLoginUser().getUserCode());
				ECNioClient client = ClientContext.getContext().getClient() ;
				try{
					client.sendMessage(packet) ;
				}catch(Exception e1){
					e1.printStackTrace() ;
				}
			}
		});
		next.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		next.setMargin(new Insets(2, 2, 2, 2));
		next.setBounds(324, 4, 61, 23);
		panel_1.add(next);

		JButton cancel = new JButton("取消");
		cancel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		cancel.setMargin(new Insets(2, 2, 2, 2));
		cancel.setBounds(389, 4, 61, 23);
		panel_1.add(cancel);

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		panel_2.setBorder(UIManager.getBorder("FormattedTextField.border"));
		panel_2.setBounds(0, 0, 119, 276);
		contentPane.add(panel_2);
		panel_2.setLayout(null);

		JLabel head = new JLabel("");
		System.out.println(user[0]);
		head.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(user[0])));
		head.setBounds(10, 0, 108, 108);
		panel_2.add(head);

		JLabel nickname = new JLabel(user[1]);
		nickname.setFont(new Font("微软雅黑", Font.BOLD, 12));
		nickname.setBounds(10, 120, 84, 15);
		panel_2.add(nickname);

		JLabel userCode = new JLabel(user[2]);
		userCode.setFont(new Font("微软雅黑", Font.BOLD, 12));
		userCode.setBounds(10, 150, 84, 15);
		panel_2.add(userCode);

		JLabel sex = new JLabel("性别：" + user[3]);
		sex.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		sex.setBounds(10, 180, 84, 15);
		panel_2.add(sex);

		String age = user[4] ;
		if(age == null || "".equals(age)) {
			age="" ;
		}
		JLabel ageLab = new JLabel("年龄：" + age);
		ageLab.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		ageLab.setBounds(10, 210, 84, 15);
		panel_2.add(ageLab);

	}

	public JFrame getFrame() {
		return this ;
	}
	
	public void addInfo(String groupName, String groupId) {
		Formater formater = new Formater(groupName, groupId) ;
		model = (DefaultComboBoxModel<Formater>) group.getModel() ; 
		model.addElement(formater) ;
		model.setSelectedItem(formater) ;
		group.setModel(model) ;
		createGroupD.dispose() ;
		getFrame().repaint() ;
		
	}
	
	public boolean isExistGroup(String groupName) {
		
		for(int i = 0; i < group.getModel().getSize(); i++) {
			if(group.getModel().getElementAt(i).getGroupName().equals(groupName)){
				return true ;
			}
		}
		return false ;
	}
}
